{% extends 'base.html' %}
{% block title %}成为机构{% endblock %}
{% block content %}
<div class="main-content container" style="background: url(../static/images/form-bg.jpg) no-repeat center top;" >
    <div class="inner-content">
    {% if is_company_user %}
        <div class="container">
            <div class="row">
                <div class="span4">
                    <div class="alert alert-success">
                        <strong>您当前登录的用户已经是机构成员了，无需再注册</strong><a href="/bs/set{% if request.session.uid %}?uid={{ request.session.uid }}{% else %}{% endif %}"><b>点此</b></a>出题
                        <a onclick="history.back(-1)" style="float:right"> <span class="glyphicon glyphicon-menu-left"></span><strong>返回上一页</strong></a>
                    </div>
                </div>
            </div>
        </div>
    {% else %}
    <div class="row" style="margin-top: 10%;" >
        <div class="col-md-3"></div>
        <div class="col-md-5" style="font-size:16px">
            <div class="panel panel-info">
                <div class="panel-heading"><h3 class="panel-title">注册成为机构</h3></div>
                <div class="panel-body">
                    <form id="bizRegistry" class="form-group">
                        <label for="bizEmail">邮箱</label>
                        <input type="text" class="form-control" id="bizEmail" placeholder="填写机构邮箱" />
                        <label for="bizCompanyName">名称</label>
                        <input type="text" class="form-control" id="bizCompanyName" placeholder="填写机构名称" />
                        <label for="bizCompanyType">类型</label>
                        <select id="bizCompanyType" class="form-control">
                            {% for k, v in types.items %}
                                <option value="{{ k }}">{{ v }}</option>
                            {% endfor %}
                        </select>
                        <label for="bizUsername">联系人</label>
                        <input type="text" class="form-control" id="bizUsername" placeholder="填写机构联系人" />
                        <label for="bizPhone">手机号</label>
                        <input type="text" class="form-control" id="bizPhone" placeholder="填写联系人手机" />
                        <input type="submit" id="bizSubmit" class="btn btn-primary" value="注册机构" style="float: right;margin-top: 20px" />
                    </form>
                </div>
            </div>
        </div>
        <div class="col-md-4"></div>
    </div>
    {% endif %}
    </div>
</div>
    <script type="text/javascript">
    $('#bizSubmit').click(function () {
        var email = $('#bizEmail').val();
        var name = $('#bizCompanyName').val();
        var type = $('#bizCompanyType').val();
        var username = $('#bizUsername').val();
        var phone = $('#bizPhone').val();
        if(!email.match('^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$')) {
            $('#bizEmail').val('');
            $('#bizEmail').attr('placeholder', '邮箱格式错误');
            $('#bizEmail').css('border', '1px solid red');
            return false;
        }else{
            $('#bizEmail').css('border', '1px solid #C1FFC1');
        }
        if(!(name.match('^[a-zA-Z0-9_\\u4e00-\\u9fa5]{4,20}$'))) {
            $('#bizCompanyName').val('');
            $('#bizCompanyName').attr('placeholder', '请填写4-20中文字母数字或者下划线机构名称');
            $('#bizCompanyName').css('border', '1px solid red');
            return false;
        }else{
            $('#bizCompanyName').css('border', '1px solid #C1FFC1');
        }
        if(!(username.match('^[\u4E00-\u9FA5A-Za-z]+$'))){
            $('#bizUsername').val('');
            $('#bizUsername').attr('placeholder', '联系人姓名应该为汉字或大小写字母');
            $('#bizUsername').css('border', '1px solid red');
            return false;
        }else{
            $('#bizUsername').css('border', '1px solid #C1FFC1');
        }
        if(!(phone.match('^1[3|4|5|8][0-9]\\d{4,8}$'))){
            $('#bizPhone').val('');
            $('#bizPhone').attr('placeholder', '手机号不符合规则');
            $('#bizPhone').css('border', '1px solid red');
            return false;
        }else{
            $('#bizPhone').css('border', '1px solid #C1FFC1');
        }
        $.ajax({
            url: '/api/checkbiz',
            type: 'get',
            data: {
                'email': email
            },
            dataType: 'json',
            success: function (res) {
                if(res.status === 200) {
                    if(res.data.bizaccountexists) {
                        alert('您的账户已存在，请直接登录');
                        window.location.href = '/index';
                    }
                    else if(res.data.userexists && !res.data.bizaccountexists) {
                        if(confirm('您的邮箱已被注册为普通用户，我们将会为您绑定该用户。')){
                            bizPost(email, name, type, username, phone, 1);
                            window.location.href = '/biz/notify?email=' + email + '&bind=1';
                        }else {
                            window.location.href = '/index{% if request.session.uid %}?uid={{ request.session.uid }}{% else %}{% endif %}';
                        }
                    }
                    else{
                        bizPost(email, name, type, username, phone, 2);
                        window.location.href = '/biz/notify?email=' + email;
                    }
                }
            }
        });
        function bizPost(email, name, type, username, phone, flag) {
            $.ajax({
                url: '/api/regbiz',
                data: {
                    'email': email,
                    'name': name,
                    'type': type,
                    'username': username,
                    'phone': phone,
                    'flag': flag
                },
                type: 'post',
                dataType: 'json'
            })
        }
    });
    </script>
{% endblock %}